package com.zHan.system.controller.system;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zHan.common.result.Result;
import com.zHan.model.dto.AssginRoleDto;
import com.zHan.model.dto.SysRoleQueryDto;
import com.zHan.model.system.SysRole;
import com.zHan.model.system.SysUser;
import com.zHan.system.anno.Log;
import com.zHan.system.menu.BusinessType;
import com.zHan.system.service.ISysRoleService;
import com.zHan.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: SysRoleServiceController
 * Description: 角色管理
 * date: 2023/6/19 0019 0:16
 *
 * @author zHan
 */
@RestController
@Slf4j
@RequestMapping("/admin/system/sysRole")
@Api(tags = "角色管理接口")
public class SysRoleController {
    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 获取全部角色列表
     *
     * @return
     */
    @GetMapping("/findAll")
    @ApiOperation(value = "获取全部角色列表")
    public Result findAll() {
        List<SysRole> roleList = sysRoleService.list();
        if (roleList != null) {
            return Result.ok(roleList);
        }
        return Result.fail();
    }

    /**
     * 分页条件查询
     *
     * @param page
     * @param pageSize
     * @param roleQueryVo
     * @return
     */
    @GetMapping("{page}/{pageSize}")
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation(value = "用户分页条件查询")
    public Result findPageQueryRole(@ApiParam(name = "page", value = "当前页码", required = true)
                                    @PathVariable Long page,
                                    @ApiParam(name = "pageSize", value = "每页记录数", required = true)
                                    @PathVariable Long pageSize,
                                    @ApiParam(name = "roleQueryVo", value = "查询对象")
                                    SysRoleQueryDto roleQueryVo) {

        Page<SysRole> pageInfo = sysRoleService.selectPageQuery(page, pageSize, roleQueryVo);
        //返回
        return Result.ok(pageInfo);
    }

    /**
     * 删除角色
     *
     * @param id
     * @return
     */
    @DeleteMapping("remove/{id}")
    @ApiOperation(value = "删除角色")
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    public Result removeRole(@PathVariable Long id) {
        boolean isSuccess = sysRoleService.removeRoleById(id);
        if (isSuccess) {
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 添加角色
     *
     * @param role
     * @return
     */
    @PostMapping("save")
    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @ApiOperation(value = "添加角色")
    @Log(title = "角色管理", businessType = BusinessType.INSERT)
    public Result save(@RequestBody SysRole role) {
        Map<String, Object> map = new HashMap<>();
        boolean isSuccess = sysRoleService.save(role);
        if (isSuccess) {
            String id = role.getId();
            String roleName = role.getRoleName();
            map.put("id", id);
            map.put("roleName", roleName);
            return Result.ok(map);
        }
        return Result.fail();
    }

    /**
     * 获得单个角色
     *
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    @ApiOperation(value = "获取单个角色")
    public Result get(@PathVariable Long id) {
        SysRole role = sysRoleService.getById(id);
        if (role != null) {
            return Result.ok(role);
        }
        return Result.fail();
    }

    /**
     * 修改角色
     *
     * @param role
     * @return
     */
    @PutMapping("/update")
    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @ApiOperation(value = "修改角色")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    public Result updateById(@RequestBody SysRole role) {
        boolean isSuccess = sysRoleService.updateById(role);
        if (isSuccess) {
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 批量删除
     *
     * @param idList
     * @return
     */
    @DeleteMapping("/batchRemove")
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation(value = "批量删除")
    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    public Result batchRemove(@RequestBody List<Long> idList) {
        boolean isSuccess = sysRoleService.removeRoleByIds(idList);
        if (isSuccess) {
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 获取用户的角色数据
     *
     * @param userId
     * @return
     */
    @GetMapping("/toAssign/{userId}")
    @ApiOperation(value = "获取用户的角色数据")
    public Result toAssign(@PathVariable String userId) {
        Map<String, Object> map = sysRoleService.selectRolesByUserId(userId);
        if (!map.isEmpty()) {
            return Result.ok(map);
        }
        return Result.fail();
    }

    /**
     * 给用户分配角色
     *
     * @param assginRoleDto
     * @return
     */
    @PostMapping("/doAssign")
    @ApiOperation(value = "给用户分配角色")
    @Log(title = "角色管理", businessType = BusinessType.OTHER)
    public Result doAssign(@RequestBody AssginRoleDto assginRoleDto) {
        boolean isSuccess = sysRoleService.assignRoleToUser(assginRoleDto);
        SysUser sysUser = sysUserService.getById(assginRoleDto.getUserId());
        if (isSuccess) {
            return Result.ok(sysUser);
        }
        return Result.fail();
    }

}
